#!perl unit test
use warnings;
use lib 'lib';
use lib 't/mock';
use Test::More tests => 18;
use Data::Dumper;

use UserAgent qw(get prepare_testfile);
use Bunnies::InterListed qw(parse fetch loop);

my $testfile = prepare_testfile();

my $ua = UserAgent->new(file=>$testfile);

my $qcibc = Bunnies::QuoteCIBC->new( ua=>$ua );
#original format
#<td nowrap><p><img src="images/up_arrow.gif">0.88</td>

my @ca_test = (
    {symbol=>"C1", "last"=>1.09, net=>0.1, bid=>1.05, ask=>1.2, voulme=>1_100  ,     volume_str=>"1,100"  }, 
    {symbol=>"C2", "last"=>2.21, net=>0.2, bid=>2.05, ask=>2.2, voulme=>2_200,       volume_str=>"2,200"  }, 
    {symbol=>"C3", "last"=>3.23, net=>0.3, bid=>3.05, ask=>3.2, voulme=>23_200,      volume_str=>"23,200"  }, 
    {symbol=>"C4", "last"=>4.34, net=>-0.4,bid=>4.05, ask=>4.2, voulme=>443_200,     volume_str=>"443,200"  }, 
    {symbol=>"C5", "last"=>5.45, net=>0.5, bid=>5.05, ask=>5.2, voulme=>5_444_200,   volume_str=>"5,444,200"  }, 
    {symbol=>"C6", "last"=>6.56, net=>0.6, bid=>6.05, ask=>6.2, voulme=>6_555_200,   volume_str=>"6,555,200"  }, 
    {symbol=>"C7", "last"=>7.67, net=>-0.7,bid=>7.05, ask=>7.2, voulme=>7_666_200,   volume_str=>"7,666,200"  }, 
    {symbol=>"C8", "last"=>8.78, net=>0.8, bid=>8.05, ask=>8.2, voulme=>778_200,     volume_str=>"778,200"  }, 
    {symbol=>"C9", "last"=>9.89, net=>0.9, bid=>9.05, ask=>9.2, voulme=>89_200,      volume_str=>"89,200"  } 
);

my @us_test = (
    {symbol=>"U1", "last"=>1.0, net=>0.1, bid=>0.96, ask=>1.08, voulme=>1_100  ,     volume_str=>"1,100"  }, 
    {symbol=>"U2", "last"=>2.0, net=>0.2, bid=>1.96, ask=>2.08, voulme=>2_200,       volume_str=>"2,200"  }, 
    {symbol=>"U3", "last"=>3.0, net=>0.3, bid=>2.96, ask=>3.08, voulme=>23_200,      volume_str=>"23,200"  }, 
    {symbol=>"U4", "last"=>4.0, net=>-0.4,bid=>3.96, ask=>4.08, voulme=>443_200,     volume_str=>"443,200"  }, 
    {symbol=>"U5", "last"=>5.0, net=>0.5, bid=>4.96, ask=>5.08, voulme=>5_444_200,   volume_str=>"5,444,200"  }, 
    {symbol=>"U6", "last"=>6.0, net=>0.6, bid=>5.96, ask=>6.08, voulme=>6_555_200,   volume_str=>"56,555,200"  }, 
    {symbol=>"U7", "last"=>7.0, net=>-0.7,bid=>6.96, ask=>7.08, voulme=>7_666_200,   volume_str=>"7,666,200"  }, 
    {symbol=>"U8", "last"=>8.0, net=>0.8, bid=>7.96, ask=>8.08, voulme=>778_200,     volume_str=>"778,200"  }, 
    {symbol=>"U9", "last"=>9.0, net=>0.9, bid=>8.96, ask=>9.08, voulme=>89_200,      volume_str=>"89,200"  } 
);

foreach my $onetest (@ca_test) {
    #printf STDERR Dumper($onetest);
    prepare_testfile($onetest);
    my $quote = $qcibc->fetch("dontcare", $onetest->{symbol});
    cmp_ok($quote->{last},  'eq', $onetest->{last},     "last parsed ok");
}

foreach my $onetest (@us_test) {
    #printf STDERR Dumper($onetest);
    prepare_testfile($onetest);
    my $quote = $qcibc->fetch("dontcare", $onetest->{symbol});
    cmp_ok($quote->{last},  'eq', $onetest->{last},     "last parsed ok");
}

my $TestList = {
    C1=>{CASymbol=>C1, USSymbol=>U1},
    C2=>{CASymbol=>C2, USSymbol=>U2},
    C3=>{CASymbol=>C3, USSymbol=>U3},
    C4=>{CASymbol=>C4, USSymbol=>U4},
    C5=>{CASymbol=>C5, USSymbol=>U5},
    C6=>{CASymbol=>C6, USSymbol=>U6},
    C7=>{CASymbol=>C7, USSymbol=>U7},
    C8=>{CASymbol=>C8, USSymbol=>U8},
    C9=>{CASymbol=>C9, USSymbol=>U9},
};

my $interlist = Bunnies::InterListed->new( quote=>$qcibc, TOP_COUNT=>3 );
$interlist->loop($TestList);
